//
//  ViewController.m
//  UIScrollView
//
//  Created by apple on 15/9/23.
//  Copyright © 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    
    scrollView.backgroundColor = [UIColor orangeColor];
    
    scrollView.delegate = self;
    
    scrollView.minimumZoomScale = 0.1;
    
    scrollView.maximumZoomScale = 10;
    
    //可以上下左右滚动；
    scrollView.contentSize = CGSizeMake(300, 300);
    
    //设置内容边框的大小；
//    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
//    //方向锁，只支持一个方向上的移动
//    scrollView.directionalLockEnabled = YES;
    
    //分页显示
//    scrollView.pagingEnabled = YES;
//    
//    //超过边界的时候是否显示回弹效果
//    
//    scrollView.bounces = NO;
    
//    //是否显示水平、垂直滚动条
//    scrollView.showsHorizontalScrollIndicator = NO;
//    
//    scrollView.showsVerticalScrollIndicator = NO;
//
    
////    点击状态栏，滚动回到顶部
//    scrollView.scrollsToTop = YES;
    
//    键盘隐藏模式

    scrollView.keyboardDismissMode = YES;
    
    [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
    
    [self.view addSubview:scrollView];
    
    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    
    greenView.backgroundColor = [UIColor greenColor];
    
    [scrollView addSubview:greenView];
    
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
    
    redView.backgroundColor = [UIColor redColor];
    
    [scrollView addSubview:redView];
}

#pragma mark - UIScrollViewDelegate

// 1、将要开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
}

//2、滚动

//只要contentOffset发生变化，就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
}

//3、将要结束拖动

//可以指定停止拖动后，UIScorllView停留的位置

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    NSLog(@"%s",__func__);
}

//4、已经结束拖动

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"%s",__func__);
    
    if (decelerate) {
        NSLog(@"开始减速了，存在减速过程");
    }
    else
    {
        NSLog(@"直接停止了");
    }
}

//5、将要开始减速

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
}

//6、已经停止减速，一定停止了滚动

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
}

//停止滚动有两种情况

//1、减速停止

//2、直接按住视图使其停止，没有减速过程


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    NSValue *value = change[NSKeyValueChangeNewKey];
    
    CGPoint point = value.CGPointValue;
    
    NSLog(@"%@",NSStringFromCGPoint(point));
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return scrollView;
}
@end
